<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
require_once ('businessfunctions.php');
validate_user();
html_header();

// unset session variable used in last input of timesheet to prevent new timesheet being affected
session_unregister('input_client');
session_unregister('input_time');
session_unregister('input_company');
session_unregister('input_job');

// find 1st and last date of the month
if (empty($_GET['new_month'])) {
	$target_day = getdate();
}
else {
	$target_day = getdate($_GET['new_month']);
}

$first_day_month = getdate(mktime(0,0,0,$target_day['mon'],1,$target_day['year']));

$last_day_month = getdate(mktime(0,0,0,$target_day['mon']+1,0,$target_day['year']));

// find transaction within the month for a particular user
$first_day = convert_unix_to_mysql_date(mktime(0,0,0,$target_day['mon'],1,$target_day['year']));
$last_day = convert_unix_to_mysql_date(mktime(0,0,0,$target_day['mon']+1,0,$target_day['year']));

if ($_GET['StaffID'] && $_SESSION['AccessLevel'] <= 2) {
	$sql = "SELECT TransactionDate, count(*) FROM billtransaction, chargetypemaster WHERE ";
	$sql .= "billtransaction.ChargeID = chargetypemaster.ChargeID AND ";
	$sql .= "StaffID = '" . $_GET['StaffID'] . "' AND ";
	$sql .= "ChargeType = 'H' AND ";
	$sql .= "TransactionDate >= '$first_day' AND ";
	$sql .= "TransactionDate <= '$last_day' ";
	$sql .= "GROUP BY TransactionDate";

	$sql_result = sql_execute($sql);
	
	while($sql_data = mysql_fetch_array($sql_result)) {
		$workdate[] = $sql_data['TransactionDate'];	
	}
}
else {
	$sql = "SELECT TransactionDate, count(*) FROM billtransaction, chargetypemaster WHERE ";
	$sql .= "billtransaction.ChargeID = chargetypemaster.ChargeID AND ";
	$sql .= "StaffID = '" . $_SESSION['UserID'] . "' AND ";
	$sql .= "ChargeType = 'H' AND ";
	$sql .= "TransactionDate >= '$first_day' AND ";
	$sql .= "TransactionDate <= '$last_day' ";
	$sql .= "GROUP BY TransactionDate";

	$sql_result = sql_execute($sql);
	
	while($sql_data = mysql_fetch_array($sql_result)) {
		$workdate[] = $sql_data['TransactionDate'];	
	}
}

// Draw calendar
echo "<br><br>$langselectstaff<br><br>";
?>
<table border="1" cellspacing='0'>
	<tr align='center' bgcolor='<? echo $color1 ?>'>
		<td bgcolor='<? echo $color2 ?>'><a href='#' onclick='lastmonth()'><? echo $langlastmonth ?></a>
		<?
		switch ($target_day['month']) {
			case "January" :
				$monthname = $langmonth1;
				break;
			case "February":
				$monthname = $langmonth2;
				break;
			case "March":
				$monthname = $langmonth3;
				break;
			case "April":
				$monthname = $langmonth4;
				break;
			case "May":
				$monthname = $langmonth5;
				break;
			case "June":
				$monthname = $langmonth6;
				break;
			case "July":
				$monthname = $langmonth7;
				break;
			case "August":
				$monthname = $langmonth8;
				break;
			case "September":
				$monthname = $langmonth9;
				break;
			case "October":
				$monthname = $langmonth10;
				break;
			case "November":
				$monthname = $langmonth11;
				break;
			default :
				$monthname = $langmonth12;
				break;	
		}
		?>
		<td colspan="5"><? echo $monthname . " " . $target_day['year'] ?>
		<td bgcolor='<? echo $color2 ?>'><a href='#' onclick='nextmonth()'><? echo $langnextmonth ?></a>
	<tr align='center' bgcolor='<? echo $color3 ?>'>
		<td width="14%"><? echo $langsunday ?>
		<td width="14%"><? echo $langmonday ?>
		<td width="14%"><? echo $langtuesday ?>
		<td width="14%"><? echo $langwednesday ?>
		<td width="14%"><? echo $langthursday ?>
		<td width="14%"><? echo $langfriday ?>
		<td width="15%"><? echo $langsaturday ?>
	<tr>
		<?
		// draw calendar here
		for ($i = 0; $i < $first_day_month['wday']; $i ++) {
			echo "<td>";
		}
		$weekday = $first_day_month['wday'];
		for ($i = 1; $i <= $last_day_month['mday']; $i ++) {
			
			// change backgroud color is timesheet data already on that day
			$timestamp = mktime(0,0,0,$target_day['mon'],$i,$target_day['year']);
			$currentdate = convert_unix_to_mysql_date($timestamp);
			
			$had_data = 0;
			if (isset($workdate)) {
				foreach ($workdate as $date) {
					if ($date == $currentdate) $had_data = 1;
				}
			}
			
			if ($had_data) echo "<td bgcolor='pink' align='center'>";
			else echo "<td align='center'>";
			
			echo "<a href='#' onclick='timesheet($timestamp)'>$i</a>";			
			$weekday ++;
			if 	($weekday > 6) {
				echo "<tr>";
				$weekday = 0;	
			}
		}
		?>
</table>
<br>
<form name = 'StaffList'>
<? echo $langstaffid ?> : 
<select name = 'StaffID' onchange='refreshuser()'>
	<?
	if ($_SESSION['AccessLevel'] <= 2) {
		echo "\n<option value = ''>";
		
		// create staff list drop down box
		$staff_list = get_staff_list();
		if ($staff_list) {
			foreach ($staff_list as $member) {
				echo "\n<option value = '" . $member['StaffID'] . "' ";
				if ($_GET['StaffID'] == $member['StaffID']) {
					echo "SELECTED";
				}
				else {
					if ($member['StaffID'] == $_SESSION['UserID']) echo "SELECTED ";
				}
				echo ">" . $member['StaffID'] . " - " . $member['StaffName'] . " - " . $member['StaffGrade'];
			}
		}
	}
	else {
		echo "\n<option value = '" . $_SESSION['UserID'] . "'>" . $_SESSION['UserID'];
	}
	?>
	</select>
</form>

<script>
	function getuser() {
		StaffIndex = document.StaffList.StaffID.selectedIndex;	
		StaffValue = document.StaffList.StaffID.options[StaffIndex].value;
		return StaffValue;	
	}
	
	function refreshuser() {
		text1 = 'StaffID=' + getuser();
		text2 = 'new_month=<? echo mktime(0,0,0,$target_day['mon'],1,$target_day['year']) ?>';
		this.location='calendar.php?' + text1 + '&' + text2 ;
	}
	
	function nextmonth() {
		text1 = 'StaffID=' + getuser();
		text2 = 'new_month=<? echo mktime(0,0,0,$target_day['mon']+1,1,$target_day['year']) ?>';
		this.location='calendar.php?' + text1 + '&' + text2 ;
	}
	
	function lastmonth() {
		text1 = 'StaffID=' + getuser();
		text2 = 'new_month=<? echo mktime(0,0,0,$target_day['mon'],0,$target_day['year']) ?>';
		this.location='calendar.php?' + text1 + '&' + text2 ;
	}
	
	function timesheet(input_date) {
		userid = getuser();
		if (! userid) {
			alert("No user selected");
			exit;
		}
		text1 = 'StaffID=' + getuser();
		text2 = 'inputdate=' + input_date;
		this.location='timesheet.php?new=1&' + text1 + '&' + text2 ;	
	}
</script>
	
		